<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png" />
      </div>
      <el-form
        ref="form"
        :model="loginForm"
        :rules="loginFormRules"
        class="login_form"
      >
        <el-form-item prop="name">
          <el-input
            prefix-icon="el-icon-user"
            v-model="loginForm.name"
          ></el-input>
        </el-form-item>
        <el-form-item prop="pass">
          <el-input
            type="password"
            prefix-icon="el-icon-lock"
            v-model="loginForm.pass"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        name: '',
        pass: '',
        testNum: 1,
      },
      loginFormRules: {
        name: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
        ],
        pass: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          {
            min: 6,
            max: 12,
            message: '长度在 6 到 12 个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  created() {
    console.log(this.$http)
  },
  mounted() {
    this.keyupLogin()
  },

  methods: {
    keyupLogin() {
      document.onkeydown = () => {
        let _key = window.event.keyCode
        if (_key === 13) {
          console.log(this)
          this.login()
        }
      }
    },
    reset() {
      this.$refs.form.resetFields()
    },
    login() {
      this.$refs.form.validate(async (valid) => {
        //console.log(valid)
        if (!valid) {
          return
        }
        //发请求成功跳转到首页。
        // const { data: res } = await this.$http.post('/login', this.loginForm)
        window.sessionStorage.setItem('token', 'admin') //登录成功存储token
        this.$message({
          type: 'info',
          message: `登录成功`,
        })
        this.$router.push('/home')
        this.$bus.$emit('handle', {
          name: this.loginForm.name,
          pass: this.loginForm.pass,
        })
      })
    },
  },
}
</script>

<style scoped>
.login_container {
  background: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -225px;
  margin-top: -150px;
}
.avatar_box {
  width: 130px;
  height: 130px;
  border: 1px solid #ccc;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #2b4b6b;
  position: absolute;
  left: 50%;
  transform: translate(-50%);
  top: -65px;
  background-color: #fff;
}
.avatar_box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  float: right;
}
</style>
